import isGeneratorFn from 'is-generator-fn';
import createModel from '../createModel';
import { SERVICES_KEY } from '../constants';
/**
 * @description 标记namespace
 * @param {@} name
 */
function namespace(name) {
  return function desc(target) {
    // eslint-disable-next-line new-cap
    const ins = new target();
    const effects = Object.keys(ins).reduce((next, key) => {
      if (isGeneratorFn(ins[key])) {
        next[key] = ins[key];
      }
      return next;
    }, {});

    const { state, reducers, subs, subscriptions } = ins;
    return createModel({
      subs: subs || subscriptions,
      state,
      effects,
      reducers,
      namespace: name,
      [SERVICES_KEY]: ins[SERVICES_KEY],
    });
  };
}

export default namespace;
